package br.ita.battlefarm.views;

import br.ita.battlefarm.core.Game;
import br.ita.battlefarm.moves.Move;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MoveView extends LinearLayout {
	
	private Move _move;
	private TextView movesCount;
	
	public void setMove(Move move){
		_move = move;
	}
	
	public Move getMove(){
		return _move;
	}

	public MoveView(Context context, Move move) {
		super(context);
		setMove(move);
        setOrientation(LinearLayout.HORIZONTAL);
        ImageButton button = new ImageButton(context);
        button.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), move.getBackgroundRes()));
        LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        
        button.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				switch(event.getAction()){
				case MotionEvent.ACTION_UP:
					if(!_move.isMoveEnabled() && _move.hasMovesLeft()){
						_move.enableMove();
						Game.getInstance().getMovesListView().setSelectedMove(_move);
					}
					invalidate();
					break;
				}
				return true;
			}
		});
        
        
        
        addView(button, 45, 45);
        movesCount = new TextView(context);
        movesCount.setText(move.getHowManyMovesLeftText());
        movesCount.setTextSize(20);
        movesCount.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        addView(movesCount, layoutParams);
        update();
	}
	
	public void update(){
		movesCount.setText(_move.getHowManyMovesLeftText());
		if(_move.isMoveEnabled()){
			setBackgroundColor(Color.BLUE);
		}
		else{
			setBackgroundColor(Color.BLACK);
		}
	}

}
